package com.wwwarehouse.resource_center.fragment.rules;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.tools.DialogTools;
import com.wwwarehouse.resource_center.R;
import com.wwwarehouse.resource_center.constant.ResourceConstant;
import com.wwwarehouse.resource_center.customView.AutoClickButton;
import com.wwwarehouse.resource_center.eventbus_event.RulesEvent;

import java.util.HashMap;
import java.util.Map;

import de.greenrobot.event.EventBus;


/**
 * @author Joseph on 2017/8/9.
 *         <p/>
 *         新建规则名称
 */

public class SetRuleNameFragment extends TheParentFragment {

    private AutoClickButton mBtn;
    private String mRuleName;
    private EditText mEtText;
    private String ruleTitle ;
    private String businessId;
    @Override
    protected int getContentId() {
        return R.layout.fragment_rule_set_rule_name;
    }

    @Override
    protected void init(View view) {
        mBtn = findView(view, R.id.confirm_btn);
        mBtn.setEnabled(false);
        mEtText = findView(view, R.id.set_rule_name);
        mBtn.bindEditText(mEtText);
    }

    @Override
    protected void loadDatas() {
        Bundle bundle = getArguments();
        if (bundle!=null){
            mRuleName = bundle.getString("mRuleName");
            ruleTitle = bundle.getString("RuleTitle");
            businessId = bundle.getString("businessId");
            mActivity.setTitle(ruleTitle);
            mEtText.setText(mRuleName);
        }
        mBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Map map = new HashMap();
                map.put("ruleName", mEtText.getText().toString().trim());
                map.put("businessId", businessId);
                httpPost(ResourceConstant.RULE_NAME_IS_EXIST, map, 1, true, "");
            }
        });
    }

    @Override
    public void setTitle() {
        if (peekFragment() instanceof SetRuleNameFragment)
            mActivity.setTitle(ruleTitle);
    }


    @Override
    public void onSuccess(CommonClass commonClass, int what) {
        switch (what) {
            case 1:
                if (TextUtils.equals("0", commonClass.getCode())) {
                    boolean isUser = (boolean) commonClass.getData();
                    if (!isUser) {
                        EventBus.getDefault().post(new RulesEvent(RulesEvent.RULE_NAME, mEtText.getText().toString().trim(), "RULE_NAME"));
                        popFragment();
                    } else {
                        DialogTools.getInstance().showCustomWarningNoTitle(mActivity, "规则名称已存在,请重新输入", "我知道了", true, null);
                    }
                }

                break;
        }
    }

    @Override
    public void onReLoad(int code) {

    }

}
